package com.city.ajax.party;

import com.city.config.Nginx;
import com.city.domain.party.*;
import com.city.domain.party.format.Abroad;
import com.city.domain.party.format.AddPerson;
import com.city.domain.pub.PubArticle;
import com.city.domain.pub.PubFilepath;
import com.city.domain.pub.PubUser;
import com.city.repository.party.PartyMembershipRepository;
import com.city.repository.pub.PubFilepathRepository;
import com.city.service.party.*;
import com.city.service.party.impl.PartyOrelationshipServiceImpl;
import com.city.service.pub.PubArticleService;
import com.city.service.pub.PubUserService;
import com.city.service.pub.SaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.util.*;

import static com.city.utils.WordToPdfUtils.Conversion;

@Controller
@RequestMapping("/ajax/party/admin")
public class partyAdminAjax {


    @Autowired
    private PartyBranchService partyBranchService;

    @Autowired
    private PubUserService pubUserService;

    @Autowired
    private SaveService saveService;

    @Autowired
    private PartyMembershipService partyMembershipService;

    @Autowired
    private PubArticleService pubArticleService;

    @Autowired
    private PartyOrelationshipService partyOrelationshipService;

    @Autowired
    private PartyFahuService partyFahuService;

    @Autowired
    private PartyFlowoutService partyFlowoutService;

    @Autowired
    private PartyApplyService partyApplyService;

    @Autowired
    private ReportPersonService reportPersonService;

    @Autowired
    private ReportGroupService reportGroupService;

    @Autowired
    private PubFilepathRepository pubFilepathRepository;

    @Autowired
    private PartyFabroadService partyFabroadService;

    @Autowired
    private PartyFaboradFamilyService partyFaboradFamilyService;

    @Autowired
    private PartyLessonService partyLessonService;

    @Autowired
    private PartyStudentService partyStudentService;

    @Autowired
    private PartyStudentLessonService partyStudentLessonService;

    @Autowired
    private PartyVideoService videoService;

    /**
     * @author  王宇
     * @date 2018/7/26
     * @description  保存添加的党组织信息
     */
    @PostMapping("/addgroup")
    @ResponseBody
    public Map<String,Object> getGroup(@RequestBody PartyBranch partyBranch){
        Map<String,Object> map = new HashMap<>();
        partyBranchService.save(partyBranch);
        map.put("code",0);
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/7/27
     * @description  获取党组织列表
     */
    @PostMapping("/grouplist")
    @ResponseBody
    public Map<String,Object> groupList(@Param("page") Integer page,
                                      @Param("limit") Integer limit){
        Map<String,Object> map = new HashMap<>();
        List<PartyBranch> partyBranchList = partyBranchService.findAll(page, limit);

        map.put("code", 0);
        map.put("msg", "成功");
        map.put("count",partyBranchService.count());
        map.put("data",partyBranchList);

        return map;
    }

    /**
     * @author  王宇
     * @date 2018/7/27
     * @description  获取单个党组织的详细信息
     */
    @PostMapping("/getOneGroup")
    @ResponseBody
    public Map<String,Object> getOneGroup(@Param("id") int id){
        Map<String,Object> map = new HashMap<>();
        PartyBranch partyBranch = partyBranchService.findById(id);

        map.put("data",partyBranch);
        map.put("code",0);
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/7/27
     * @description  党组织选择请求
     */
    @PostMapping("/branches")
    @ResponseBody
    public Map<String,Object> branches(@Param("check") int check){
        Map<String,Object> map = new HashMap<>();
        if(check==1024){
            List<PartyBranch> partyBranchList = partyBranchService.findAllPartyBranch();


            map.put("code",0);
            map.put("data",partyBranchList);
        }

        return map;
    }

    /**
     * @author  王宇
     * @date 2018/7/27
     * @description  添加组织人员
     */
    @PostMapping("/addperson")
    @ResponseBody
    public Map<String,Object> addPerson(@RequestBody AddPerson addPerson){
        Map<String,Object> map = new HashMap<>();
        PubUser pubUser = pubUserService.getUserByIdCard(addPerson.getIdCard());
        if(pubUser==null){
            PubUser pubUser1 = new PubUser();
            pubUser1.setName(addPerson.getName());
            pubUser1.setSex(Integer.parseInt(addPerson.getSex()));
            pubUser1.setNation(Integer.parseInt(addPerson.getNation()));
            pubUser1.setBorn(addPerson.getBorn());
            pubUser1.setIdCard(addPerson.getIdCard());
            pubUser1.setHrResiter(addPerson.getHrResiter());
            pubUser1.setBornAddress(addPerson.getBornAddress());
            if(addPerson.getWork()=="是"){
                pubUser1.setWork("已退休");
            }else {
                pubUser1.setWork("有工作");
            }
            pubUser1.setMarriage(addPerson.getMarriage());
            pubUser1.setEducation(addPerson.getEducation());
            pubUser1.setLivein(addPerson.getLivein());
            pubUser1.setSchool(addPerson.getSchool());
            pubUser1.setTellphone(addPerson.getTelephone());
            pubUser1.setMajor(addPerson.getMajor());
            pubUser1.setEmail(addPerson.getEmail());
            pubUser1.setQq(addPerson.getQq());
            pubUser1.setWeixin(addPerson.getWeixin());
            saveService.save(pubUser1);

            PubUser p2 = pubUserService.getUserByIdCard(pubUser1.getIdCard());
            int userId = p2.getId();

            PartyMembership pm = new PartyMembership();
            pm.setId(addPerson.getId());

            pm.setUserId(userId);
            pm.setName(addPerson.getName());
            pm.setJoinDate(addPerson.getJoinDate());
            pm.setTelephone(addPerson.getPtelephone());
            pm.setPersonalPhoto(addPerson.getPersonalPhoto());
            pm.setBranch(addPerson.getBranch());
            pm.setPersonCategory(addPerson.getPersonCategory());
            pm.setPositionInparty(addPerson.getPositionInparty());
            pm.setBecomefullDate(addPerson.getBecomefullDate());
            pm.setPartyMoney(addPerson.getPartyMoney());
            pm.setMembershipStatus(addPerson.getMembershipStatus());
            pm.setIsOut(addPerson.getIsOut());
            pm.setIsFlow(addPerson.getIsFlow());
            pm.setIsCollegeOfficial(addPerson.getIsCollegeOfficial());
            pm.setIsTwoRepresents(addPerson.getIsTwoRepresents());
            pm.setIsOutcontact(addPerson.getIsOutcontact());
            pm.setWorkplace(addPerson.getWorkplace());
            pm.setTechnicalTitle(addPerson.getTechnicalTitle());
            pm.setPost(addPerson.getPost());
            pm.setAdministrativePosition(addPerson.getAdministrativePosition());
            partyMembershipService.save(pm);
            map.put("code",0);
        }else if(pubUser!=null){
            PubUser p2 = pubUserService.getUserByIdCard(pubUser.getIdCard());
            int userId = p2.getId();

            PartyMembership pm = new PartyMembership();

            pm.setId(addPerson.getId());
            pm.setUserId(userId);
            pm.setName(addPerson.getName());
            pm.setJoinDate(addPerson.getJoinDate());
            pm.setPersonalPhoto(addPerson.getPersonalPhoto());
            pm.setBranch(addPerson.getBranch());
            pm.setPersonCategory(addPerson.getPersonCategory());
            pm.setPositionInparty(addPerson.getPositionInparty());
            pm.setBecomefullDate(addPerson.getBecomefullDate());
            pm.setPartyMoney(addPerson.getPartyMoney());
            pm.setMembershipStatus(addPerson.getMembershipStatus());
            pm.setIsOut(addPerson.getIsOut());
            pm.setIsFlow(addPerson.getIsFlow());
            pm.setIsCollegeOfficial(addPerson.getIsCollegeOfficial());
            pm.setIsTwoRepresents(addPerson.getIsTwoRepresents());
            pm.setIsOutcontact(addPerson.getIsOutcontact());
            pm.setWorkplace(addPerson.getWorkplace());
            pm.setTechnicalTitle(addPerson.getTechnicalTitle());
            pm.setPost(addPerson.getPost());
            pm.setAdministrativePosition(addPerson.getAdministrativePosition());
            partyMembershipService.save(pm);
            map.put("code",0);
        }else {
            map.put("code",1);
        }
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/7/27
     * @description  获取党组织人员列表
     */
    @PostMapping("/personlist")
    @ResponseBody
    public Map<String,Object> getPersonList(@Param("page") Integer page,
                                            @Param("limit") Integer limit){

        Map<String,Object> map = new HashMap<>();
        List<PartyMembership> partyMemberships = partyMembershipService.findLimit(page,limit);
        map.put("msg","成功");
        map.put("code",0);
        map.put("data",partyMemberships);
        map.put("count",partyMembershipService.count());

        return map;
    }
    
    /**
     * @author  王宇
     * @date 2018/7/27
     * @description  获取单个组织人员信息
     */
    @PostMapping("/getOnePerson")
    @ResponseBody
    public Map<String,Object> getOnePerson(@Param("id") int id){
        Map<String,Object> map = new HashMap<>();
        //封装格式化对象传到前台进行显示

        PartyMembership pm = partyMembershipService.findById(id);
        PubUser pubUser = pubUserService.findById(pm.getUserId());

        System.out.println(pubUser);

        AddPerson addPerson = new AddPerson();
        addPerson.setName(pubUser.getName());
        addPerson.setSex(pubUser.getSex() + "");
        addPerson.setNation(pubUser.getNation() + "");
        addPerson.setBorn(pubUser.getBorn());
        addPerson.setIdCard(pubUser.getIdCard());
        addPerson.setHrResiter(pubUser.getHrResiter());
        addPerson.setBornAddress(pubUser.getBornAddress());
        addPerson.setWork(pubUser.getWork());
        addPerson.setMajor(pubUser.getMajor());
        addPerson.setMarriage(pubUser.getMarriage());
        addPerson.setEducation(pubUser.getEducation());
        addPerson.setLivein(pubUser.getLivein());
        addPerson.setSchool(pubUser.getSchool());
        addPerson.setTelephone(pubUser.getTellphone());
        addPerson.setEmail(pubUser.getEmail());
        addPerson.setQq(pubUser.getQq());
        addPerson.setWeixin(pubUser.getWeixin());

        addPerson.setJoinDate(pm.getJoinDate());
        addPerson.setPtelephone(pm.getTelephone());
        addPerson.setPersonalPhoto(pm.getPersonalPhoto());
        addPerson.setBranch(pm.getBranch());
        addPerson.setPersonCategory(pm.getPersonCategory());
        addPerson.setPositionInparty(pm.getPositionInparty());
        addPerson.setBecomefullDate(pm.getBecomefullDate());
        addPerson.setPartyMoney(pm.getPartyMoney());
        addPerson.setMembershipStatus(pm.getMembershipStatus());
        addPerson.setIsOut(pm.getIsOut());
        addPerson.setIsFlow(pm.getIsFlow());
        addPerson.setIsOutcontact(pm.getIsOutcontact());
        addPerson.setIsCollegeOfficial(pm.getIsCollegeOfficial());
        addPerson.setIsTwoRepresents(pm.getIsTwoRepresents());
        addPerson.setWorkplace(pm.getWorkplace());
        addPerson.setTechnicalTitle(pm.getTechnicalTitle());
        addPerson.setPost(pm.getPost());
        addPerson.setAdministrativePosition(pm.getAdministrativePosition());


        map.put("code",0);
        map.put("data",addPerson);


        return map;
    }

    /**
     * @author  王宇
     * @date 2018/7/27
     * @description  删除组织
     */
    @PostMapping("/delGroup")
    @ResponseBody
    public Map<String,Object> delGroup(@Param("id") int id){
        Map<String,Object> map = new HashMap<>();
        partyBranchService.deleteById(id);
        map.put("code",0);
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/7/27
     * @description  删除组织人员
     */

    @PostMapping("/delPerson")
    @ResponseBody
    public Map<String,Object> delPerson(@Param("id") int id){
        Map<String,Object> map = new HashMap<>();
        partyMembershipService.deleteById(id);
        map.put("code",0);
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/7/28
     * @description  获取文章列表
     */
    @PostMapping("articleList")
    @ResponseBody
    public Map<String,Object> getArticleList(@Param("page") Integer page,
                                             @Param("limit") Integer limit){
        Map<String,Object> map = new HashMap<>();
        List<PubArticle> articleList = pubArticleService.findPubArticleLimit(page, limit);

        map.put("msg","成功");
        map.put("code",0);
        map.put("data",articleList);
        map.put("count",pubArticleService.count());

        return map;

    }

    /**
     * @author  王宇
     * @date 2018/7/28
     * @description  添加文章
     */
    @PostMapping("addArticle")
    @ResponseBody
    public Map<String,Object> addArticle(@RequestBody PubArticle pubArticle){
        Map<String,Object> map  = new HashMap<>();
        saveService.save(pubArticle);

        map.put("code",0);

        return map;
    }
    /**
     * @author  王宇
     * @date 2018/7/29
     * @description  删除 文章
     */
    @PostMapping("/deleteArticle")
    @ResponseBody
    public Map<String,Object> delArticle(@Param("id") int id){
        Map<String,Object> map = new HashMap<>();
        pubArticleService.deleteArticle(id);

        map.put("code",0);
        return map;
    }
    /**
     * @author  王宇
     * @date 2018/7/29
     * @description  查询一篇文章
     */
    @PostMapping("/getOneArticle")
    @ResponseBody
    public Map<String,Object> getOneArticle(@Param("id") int id){
        Map<String,Object> map = new HashMap<>();
        PubArticle pubArticle =  pubArticleService.findById(id);

        map.put("code",0);
        map.put("data",pubArticle);
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/8/4
     * @description 组织关系转接list
     */
    @PostMapping("/changelist")
    @ResponseBody
    public Map<String,Object> getChangeList(@Param("page") Integer page,
                                            @Param("limit") Integer limit){
        Map<String,Object> map = new HashMap<>();
        List<PartyOrelationship> partyOrelationships = partyOrelationshipService.findByLimt(page, limit);
        map.put("msg","成功");
        map.put("code",0);
        map.put("data",partyOrelationships);
        map.put("count",partyOrelationshipService.count());

        return map;
    }

    @PostMapping("/getOneChange")
    @ResponseBody
    public Map<String,Object> getOneChange(@Param("id") int id){
        Map<String,Object> map = new HashMap<>();
        PartyOrelationship partyOrelationship = partyOrelationshipService.findById(id);
        map.put("data",partyOrelationship);
        map.put("code",0);
        return map;
    }


    /**
     * @author  王宇
     * @date 2018/8/4
     * @description  删除转接记录
     */
    @PostMapping("/delChange")
    @ResponseBody
    public Map<String,Object> delChange(@Param("id") int id){
        Map<String,Object> map = new HashMap<>();
        partyOrelationshipService.delete(id);
        map.put("code",0);
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/8/4
     * @description  流动挂靠列表
     */

    @PostMapping("/ldonglist")
    @ResponseBody
    public Map<String,Object> getLdong(@Param("page") Integer page,
                                       @Param("limit") Integer limit){
        Map<String,Object> map = new HashMap<>();
        List<PartyFahu> fahus = partyFahuService.findByPage(page, limit);

        map.put("msg","成功");
        map.put("data",fahus);
        map.put("code",0);
        map.put("count",partyFahuService.count());
        return map;
    }

    @PostMapping("/delLiudong")
    @ResponseBody
    public Map<String,Object> delLiudong(@Param("id") int id){
        Map<String,Object> map = new HashMap<>();
        partyFahuService.delete(id);
        map.put("code",0);
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/8/5
     * @description  查询单个流动记录
     */
    @PostMapping("/getOneLiudong")
    @ResponseBody
    public Map<String,Object> getOneLiudong(@Param("id") int id){
        Map<String,Object> map = new HashMap<>();
        PartyFahu partyFahu = partyFahuService.findById(id);
        map.put("code",0);
        map.put("data",partyFahu);
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/8/5
     * @description  流出申请列表
     */
    @PostMapping("/lchulist")
    @ResponseBody
    public Map<String,Object> getLchuList(@Param("page") Integer page,
                                          @Param("limit") Integer limit){
        Map<String,Object> map = new HashMap<>();
        List<PartyFlowout> flowouts = partyFlowoutService.findByPage(page, limit);
        map.put("code",0);
        map.put("msg","成功");
        map.put("data",flowouts);
        map.put("count",partyFlowoutService.count());
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/8/5
     * @description  根据id查询记录
     */
    @PostMapping("/getOneLchu")
    @ResponseBody
    public Map<String,Object> getOneLchu(@Param("id") int id){
        Map<String,Object> map = new HashMap<>();
        PartyFlowout partyFlowout = partyFlowoutService.findById(id);
        map.put("data",partyFlowout);
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/8/5
     * @description  删除记录
     */
    @PostMapping("/delLiuchu")
    @ResponseBody
    public Map<String,Object> delLiuchu(@Param("id") int id){
        Map<String,Object> map = new HashMap<>();
        partyFlowoutService.delete(id);
        map.put("code",0);
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/8/5
     * @description  入党申请list
     */
    @PostMapping("/rdsqlist")
    @ResponseBody
    public Map<String,Object> getRdsqLIst(@Param("page") Integer page,
                                          @Param("limit") Integer limit){
        Map<String,Object> map = new HashMap<>();
        List<PartyApply> partyApplies = partyApplyService.findByPage(page, limit);

//        //将1和2转换成对应的性别
//        for (int i=0;i<partyApplies.size();i++){
//            if(partyApplies.get(i).getSex()=="1"){
//
//            }
//        }

        map.put("code",0);
        map.put("msg","成功");
        map.put("data",partyApplies);
        map.put("count",partyApplyService.count());
        return map;
    }
    /**
     * @author  王宇
     * @date 2018/8/5
     * @description  查询单条入党申请记录
     */

    @PostMapping("/getOneRd")
    @ResponseBody
    public Map<String,Object> getOneRd(@Param("id") int id){
        Map<String,Object> map = new HashMap<>();
        PartyApply partyApply = partyApplyService.findById(id);
        map.put("data",partyApply);
        return map;
    }
    /**
     * @author  王宇
     * @date 2018/8/6
     * @description根据id删除记录
     */
    @PostMapping("/delrdsq")
    @ResponseBody
    public Map<String,Object> delrdsq(@Param("id") int id){
        Map<String,Object> map = new HashMap<>();
        partyApplyService.delete(id);

        map.put("code",0);
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/8/6
     * @description  个人举报list
     */

    @PostMapping("/reportperson")
    @ResponseBody
    public Map<String,Object> reportPersonList(@Param("page") Integer page,
                                               @Param("limit") Integer limit){
        Map<String,Object> map = new HashMap<>();
        List<ReportPerson> reportPersonList = reportPersonService.findByPage(page,limit);
        map.put("code",0);
        map.put("msg","成功");
        map.put("data",reportPersonList);
        map.put("count",reportPersonService.count());
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/8/6
     * @description  查询单条记录
     */
    @PostMapping("/getOneRP")
    @ResponseBody
    public Map<String,Object> getOneReportP(@Param("id") int id){
        Map<String,Object> map = new HashMap<>();
        ReportPerson rp = reportPersonService.findById(id);
        map.put("data",rp);
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/8/6
     * @description  删除举报记录
     */
    @PostMapping("/delRP")
    @ResponseBody
    public Map<String,Object> delRP(@Param("id") int id){
        Map<String,Object> map = new HashMap<>();
        reportPersonService.delete(id);
        map.put("code",0);
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/8/6
     * @description  组织举报列表
     */
    @PostMapping("/reportgroup")
    @ResponseBody
    public Map<String,Object> getRepotGrop(@Param("page") Integer page,
                                           @Param("limit") Integer limit){
        Map<String,Object> map = new HashMap<>();
        List<ReportGroup> reportGroupList = reportGroupService.findByPage(page, limit);
        map.put("code",0);
        map.put("msg","成功");
        map.put("data",reportGroupList);
        map.put("count",reportGroupService.count());
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/8/6
     * @description  单条记录
     */
    @PostMapping("/getOneRG")
    @ResponseBody
    public Map<String,Object> getOneRG(@Param("id") int id){
        Map<String,Object> map = new HashMap<>();
        ReportGroup reportGroup = reportGroupService.findById(id);
        map.put("data",reportGroup);
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/8/6
     * @description  删除记录
     */
    @PostMapping("/delRG")
    @ResponseBody
    public Map<String,Object> delRG(@Param("id") int id){
        Map<String,Object> map = new HashMap<>();
        reportGroupService.delete(id);

        map.put("code",0);
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/8/6
     * @description  找到对应的pdf 如果没有就创建
     */
    @PostMapping("/getApplyBook")
    @ResponseBody
    public Map<String,Object> getApplyBook(@Param("path") String path,
                                           @Param("time") String time){
        Map<String,Object> map = new HashMap<>();
        System.out.println(path);
        System.out.println(time);
        //拿到远程服务器文件的位置
        PubFilepath pubFilepath = pubFilepathRepository.findByIdIndex(time);

        System.out.println(pubFilepath.getFilePath());

        String s = Nginx.ABS_URL;
        String abPath = s + path;
        path =pubFilepath.getFilePath();
        System.out.println(path);
        //获取文件后缀名
        int splitIndex = abPath.lastIndexOf(".");
        String houzhui = abPath.substring(splitIndex + 1);
        String Finalpath = abPath.replace("."+houzhui,".pdf");
        File file = new File(Finalpath);

        //拿到文件名
        path = path.trim();
        String fileName = path.substring(path.lastIndexOf("/")+1);
        System.out.println("fileName = " + fileName);

        //拿到储存文件的路径名
        String savePath = abPath.replace(fileName,"").trim();
        System.out.println("savePath="+savePath);

        //设置最后存储成的pdf文件名
        String pdfName = fileName.replace("."+houzhui,".pdf");
        System.out.println("pdfNmae="+pdfName);

        //判断pdf是否存在，不存在创建，存在返回对应的路径
        if(file.exists()){
            map.put("code",0);
            map.put("data",path.replace(".doc",".pdf"));
        }else {
                fileDomainVO vo = new fileDomainVO();
                vo.setDetails(Details.DocToPDF);          //进行word转pdf
                vo.setInputfile(path);       //需要转换的word
                vo.setOutputfile(savePath);         //保存路径
                vo.setFileNameAfter(pdfName);           //转换后的文件名,自己取
                Conversion(vo);
                map.put("code",0);
                map.put("data",path.replace(".doc",".pdf"));
        }
        System.out.println(path.replace(".doc",".pdf"));
        return map;
    }


    /**
     * @author  王宇
     * @date 2018/8/8
     * @description  将pdf以流的形式发送到页面
     */
    @RequestMapping(value = "/preview")
    public void pdfStreamHandler(HttpServletRequest request,
                                 HttpServletResponse response) {


        String basePath = Nginx.ABS_URL;
        System.out.println(basePath);

        Map<String,String[]> map = request.getParameterMap();
        String fileName = null;
        for(String key : map.keySet()){
            fileName =key;
            break;
        }
        System.out.println(fileName);
        String finalPath = basePath+fileName;
        System.out.println(finalPath);
        finalPath = finalPath.replace(".doc",".pdf");
        File file = new File(finalPath);
        if (file.exists()){
            byte[] data = null;
            try {
                FileInputStream input = new FileInputStream(file);
                data = new byte[input.available()];
                input.read(data);
                response.getOutputStream().write(data);
                input.close();
            } catch (Exception e) {
//                logger.error("pdf文件处理异常：" + e.getMessage());
            }

        }else{
            return;
        }
    }

    /**
     * @author  王宇
     * @date 2018/8/8
     * @description  组织列表模糊查询
     */
    @PostMapping("/serachGroup")
    @ResponseBody
    public Map<String,Object> serarchGroup(@Param("page") Integer page,
                                           @Param("limit") Integer limit,
                                           @Param("groupNum") int groupNum,
                                           @Param("groupName") String groupName){
        Map<String,Object> map = new HashMap<>();
//        System.out.println(page);
        System.out.println(page);
        System.out.println(limit);
        if((groupName==null || groupName=="") && groupNum!=0){
            PartyBranch partyBranch = partyBranchService.findById(groupNum);
            List<PartyBranch> partyBranchList = new ArrayList<>();
            partyBranchList.add(partyBranch);
            map.put("code",0);
            map.put("data",partyBranchList);
            map.put("count",partyBranchList.size());
        }else if(groupNum==0 &&((groupName!=null || groupName!=""))){
            List<PartyBranch> partyBranchList = partyBranchService.findLikeName(groupName,page,limit);
            map.put("code",0);
            map.put("data",partyBranchList);
            map.put("count",partyBranchService.countName(groupName));

        }else if((groupName!=null || groupName!="") && groupNum!=0){
            List<PartyBranch> branches = new ArrayList<>();
            PartyBranch branch = partyBranchService.findById(groupNum);
            branches.add(branch);
            map.put("data",branches);
            map.put("code",0);
            map.put("count",branches.size());
        }else {
            List<PartyBranch> branches = partyBranchService.findAll(page, limit);
            map.put("data",branches);
            map.put("code",0);
            map.put("count",partyBranchService.count());
        }

        return map;
    }

    /**
     * @author  王宇
     * @date 2018/8/8
     * @description  人员列表模糊查询
     */
    @PostMapping("/serarchPerson")
    @ResponseBody
    public Map<String,Object> searchPerson(@Param("page") Integer page,
                                           @Param("limit") Integer limit,
                                           @Param("personNum") int personNum,
                                           @Param("personName") String personName){
        Map<String,Object> map = new HashMap<>();

        if((personName=="" || personName==null) && personNum !=0){
            PartyMembership partyMembership = partyMembershipService.findById(personNum);
            List<PartyMembership> partyMemberships = new ArrayList<>();
            partyMemberships.add(partyMembership);
            map.put("data",partyMemberships);
            map.put("count",partyMemberships.size());
            map.put("code",0);
        }else if((personName!="" || personName!=null) && personNum ==0){
            List<PartyMembership> partyMemberships = partyMembershipService.findLikeName(personName,page,limit);
            map.put("data",partyMemberships);
            map.put("count",partyMembershipService.countName(personName));
            map.put("code",0);
        }else if((personName!="" || personName!=null) && personNum !=0){
            List<PartyMembership> memberships = new ArrayList<>();
            PartyMembership membership = partyMembershipService.findById(personNum);
            memberships.add(membership);
            map.put("code",0);
            map.put("data",memberships);
            map.put("count",memberships.size());
        }else {

            List<PartyMembership> memberships = partyMembershipService.findLimit(page, limit);

            map.put("data",memberships);
            map.put("code",1);
            map.put("count",partyMembershipService.count());
        }


        return map;
    }

    /**
     * @author  王宇
     * @date 2018/8/8
     * @description  搜索文章
     */
    @PostMapping("/searchArticle")
    @ResponseBody
    public Map<String,Object> searchArticle(@Param("page") Integer page,
                                            @Param("limit") Integer limit,
                                            @Param("typeName") String typeName,
                                            @Param("theSource") String theSource){
        Map<String,Object> map = new HashMap<>();
        if((typeName=="" || typeName==null) && (theSource!="" || theSource!=null)){
            List<PubArticle> articleList = pubArticleService.findByTheSource(theSource,page,limit);
            map.put("code",0);
            map.put("data",articleList);
            map.put("count",pubArticleService.countTheSource(theSource));

        }else if((typeName!="" || typeName!=null) && (theSource=="" || theSource==null)){
            List<PubArticle> articleList = pubArticleService.findByTypeName(typeName,page,limit);
            map.put("code",0);
            map.put("data",articleList);
            map.put("count",pubArticleService.countTypeName(typeName));

        }else if((typeName!="" || typeName!=null) && (theSource!="" || theSource!=null)){
            List<PubArticle> articleList = pubArticleService.findByTypeNameAndTheSource(typeName,theSource,page,limit);
            map.put("code",0);
            map.put("data",articleList);
            map.put("count",pubArticleService.countTypeNameAndTheSource(typeName,theSource));


        }else {
            List<PubArticle> articleList = pubArticleService.findPubArticleLimit(page, limit);

            map.put("data",articleList);
            map.put("code",0);
            map.put("count",pubArticleService.count());
        }
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/8/11
     * @description  干部出国list
     */
    @PostMapping("/abroadlist")
    @ResponseBody
    public Map<String,Object> abroadList(@Param("page") Integer page,
                                         @Param("limit") Integer limit){
        Map<String,Object> map = new HashMap<>();
        List<PartyFabroad> fabroads = partyFabroadService.findByPage(page, limit);

        List<Abroad> abroads = new ArrayList<>();
//        for(int i=0; i<abroads.size();i++){
//            abroads.get(i).setAbdDestination(fabroads.get(i).getDestination());
//            abroads.get(i).setId(fabroads.get(i).getId());
//            abroads.get(i).setUpdateTime(fabroads.get(i).getUpdateTime());
//            abroads.get(i).setAbdName(fabroads.get(i).getName());
//            abroads.get(i).setAbdLevel(fabroads.get(i).getLevel());
//            abroads.get(i).setToGroup(fabroads.get(i).getCompany());
//            if(fabroads.get(i).getAbdStatus()==1){
//                abroads.get(i).setStatus("未归");
//            }else {
//                abroads.get(i).setStatus("已回国");
//            }
//
//        }

        map.put("msg","成功");
        map.put("data",fabroads);
        map.put("code",0);
        map.put("count",partyFabroadService.count());

        return map;
    }

    /**
     * @author  王宇
     * @date 2018/8/11
     * @description  详情页
     */
    @PostMapping("/getOneAbd")
    @ResponseBody
    public Map<String,Object> getOneAbd(@Param("id") int id){
        Map<String,Object> map = new HashMap<>();
        PartyFabroad partyFabroad = partyFabroadService.findById(id);
        String abroadId = partyFabroad.getTime();
        Abroad abroad = new Abroad();
        abroad.setId(partyFabroad.getId());
        abroad.setToGroup(partyFabroad.getCompany());
        abroad.setAbdName(partyFabroad.getName());
        abroad.setAbdLevel(partyFabroad.getLevel());
        abroad.setAbdSex(partyFabroad.getSex());
        abroad.setAbdHealth(partyFabroad.getHealthy());
        abroad.setAbdPost(partyFabroad.getPost());
        abroad.setAbdIdentity(partyFabroad.getPoliticalOutlook());
        abroad.setAbdDestination(partyFabroad.getDestination());
        abroad.setAbdBeginDate(partyFabroad.getFromStart());
        abroad.setAbdEndDate(partyFabroad.getFromEnd());
        abroad.setAbdTelWithIn(partyFabroad.getDomesticTell());
        abroad.setAbdTelWithOut(partyFabroad.getAbroadTell());
        abroad.setAbdFunds(partyFabroad.getFundingChannel());
        abroad.setAbdTripMode(partyFabroad.getMethod());
        abroad.setAbdRemark(partyFabroad.getRemarks());
        abroad.setUpdateTime(partyFabroad.getUpdateTime());
        if(partyFabroad.getAbdStatus()==1){
            abroad.setStatus("未归");
        }else {
            abroad.setStatus("已回国");
        }
        List<PartyFaboradFamily> infa = partyFaboradFamilyService.findByAbdId(abroadId,"国内亲属");
        List<PartyFaboradFamily> outfa = partyFaboradFamilyService.findByAbdId(abroadId,"国外亲属");
        List<String> inJob1 = new ArrayList<>();
        List<String> inName1 = new ArrayList<>();  //国内亲属名字
        List<String> inShip1 = new ArrayList<>();  // 国内亲属关系
        List<String>outJob1 = new ArrayList<>();   //国外亲属工作
        List<String> outName1 = new ArrayList<>();  //国外亲属名字
        List<String> outShip1 = new ArrayList<>();  //国外亲属关系
        //格式化操作
        for (int i=0;i<infa.size();i++){
            inName1.add(infa.get(i).getName());
            inJob1.add(infa.get(i).getCompany());
            inShip1.add(infa.get(i).getRelationship());
        }
        for (int i=0;i<outfa.size();i++){
            outName1.add(outfa.get(i).getName());
            outJob1.add(outfa.get(i).getCompany());
            outShip1.add(outfa.get(i).getRelationship());
        }
        abroad.setInJob1(inJob1);
        abroad.setInName1(inName1);
        abroad.setInShip1(inShip1);

        abroad.setOutJob1(outJob1);
        abroad.setOutName1(outName1);
        abroad.setOutShip1(outShip1);


        map.put("data",abroad);
        map.put("code",0);
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/8/11
     * @description  干部出国模糊查询
     */
    @PostMapping("/searchAbroad")
    @ResponseBody
    public Map<String,Object> searhAbroad(@Param("page") Integer page,
                                          @Param("limit") Integer limit,
                                          @Param("aimCountry") String aimCountry ,
                                          @Param("name") String name){
        Map<String,Object> map = new HashMap<>();
        if((aimCountry==""|| aimCountry==null) && (name!="" || name!=null)){
            List<PartyFabroad> fabroads = partyFabroadService.findAllByNameAndPage(name,page,limit);

            map.put("data",fabroads);
            map.put("code",0);
            map.put("count",partyFabroadService.countName(name));

        }else if((aimCountry!=""|| aimCountry!=null) && (name=="" || name==null)){
            List<PartyFabroad> fabroads = partyFabroadService.findAllByDestinationAndPage(aimCountry,page,limit);

            map.put("data",fabroads);
            map.put("code",0);
            map.put("count",partyFabroadService.countDestination(aimCountry));
        }else if((aimCountry!=""|| aimCountry!=null) && (name!="" || name!=null)){
            List<PartyFabroad> fabroads = partyFabroadService.findAllByDestinationAndNameAndPage(aimCountry,name,page,limit);



            map.put("data",fabroads);
            map.put("code",0);
            map.put("count",partyFabroadService.countDestinationAndName(aimCountry,name));
        }else {
            List<PartyFabroad> fabroads = partyFabroadService.findAll();



            map.put("data",fabroads);
            map.put("code",0);
            map.put("count",partyFabroadService.count());

        }

        return map;
    }
    /**
     * @author  王宇
     * @date 2018/8/12
     * @description  添加课程
     */
    @PostMapping("/addlesson")
    @ResponseBody
    public Map<String,Object> addLesson(@RequestBody PartyLesson partyLesson){
        Map<String,Object> map = new HashMap<>();
        System.out.println(partyLesson.getIspublic());
        partyLessonService.save(partyLesson);

        map.put("code",0);

        return map;
    }

    /**
     * @author  王宇
     * @date 2018/8/12
     * @description  课程列表
     */
    @PostMapping("/lessonlist")
    @ResponseBody
    public Map<String,Object> lessonList(@Param("page") Integer page,
                                         @Param("limit") Integer limit){
        Map<String,Object> map = new HashMap<>();
        List<PartyLesson> lessons = partyLessonService.findByPage(page, limit);
        map.put("data",lessons);
        map.put("msg","成功");
        map.put("count",partyLessonService.count());
        map.put("code",0);
        return map;
    }
    /**
     * @author  王宇
     * @date 2018/8/12
     * @description  查询单个课程
     */
    @PostMapping("/getOneLessonn")
    @ResponseBody
    public Map<String,Object> getOneLesson(@Param("id") int id){
        Map<String,Object> map = new HashMap<>();
        PartyLesson lesson = partyLessonService.findById(id);
        map.put("data",lesson);
        map.put("code",0);
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/8/12
     * @description  动态获取课程
     */
    @PostMapping("/lessons")
    @ResponseBody
    public Map<String,Object> getLessons(@Param("check") int check){
        Map<String,Object> map = new HashMap<>();
        if(check==1024){
            List<PartyLesson> lessons = partyLessonService.findAll();
            map.put("code",0);
            map.put("data",lessons);
        }
        return map;
    }
    /**
     * @author  王宇
     * @date 2018/8/12
     * @description  添加学员
     */
    @PostMapping("/addstudent")
    @ResponseBody
    public Map<String,Object> addStudent(@RequestBody PartyStudent partyStudent){
        Map<String,Object> map = new HashMap<>();
//        System.out.println(partyStudent.getBranch());
        long time = new Date().getTime();
        String s = String.valueOf(time);
        partyStudent.setTime(s);
        partyStudentService.save(partyStudent);

        map.put("time",s);
        map.put("code",0);
        return map;
    }

    @PostMapping("/addSlesson")
    @ResponseBody
    public Map<String,Object> addSlesson(@RequestBody String[] lessens){
        Map<String,Object> map = new HashMap<>();
        //拿到对应学生的唯一标识time
        String time = lessens[lessens.length-1];
        //通过time 拿到学生id  保存到学生选课关联表里面
        PartyStudent student = partyStudentService.fingByTime(time);
        int studentId = student.getId();

        for(int i=0;i<lessens.length-1;i++){
            PartyStudentLesson studentLesson = new PartyStudentLesson();
            studentLesson.setStudentId(studentId);
            studentLesson.setLessonId(Integer.parseInt(lessens[i]));
            studentLesson.setStatus(0);
            partyStudentLessonService.save(studentLesson);
//            System.out.println(lessens[i]);
        }
        //获取对应学生未完成的课程数
        int unLearned = partyStudentLessonService.countUUnleaned(studentId);
        //该学生课程总数
        int allLesson = partyStudentLessonService.countAllLesson(studentId);

        //该学生已经完成的课程数  总数 - 未完成 = 完成
        int haveLearned = allLesson-unLearned;
        student.setUnlearned(unLearned);
        student.setHaveLearn(haveLearned);
        partyStudentService.save(student);
        map.put("code",0);
        return map;
    }

    /**
     * @author  王宇
     * @date 2018/8/13
     * @description  学员list
     */
    @PostMapping("/studentlist")
    @ResponseBody
    public Map<String,Object> studenlist(@Param("page") Integer page,
                                         @Param("limit")Integer limit){
        Map<String,Object> map = new HashMap<>();
        //查询前线更新表

        List<PartyStudent> students = partyStudentService.findByPage(page, limit);

        map.put("data",students);
        map.put("msg","成功");
        map.put("count",partyStudentService.count());
        map.put("code",0);

        return map;
    }

    /**
     * @author  王宇
     * @date 2018/8/13
     * @description  查询某个学生的详细学习情况
     */
    @PostMapping("/getOneStudent")
    @ResponseBody
    public Map<String,Object> getOneStudent(@Param("id") int id){
        Map<String,Object> map = new HashMap<>();
        //学生个人信息
        PartyStudent student = partyStudentService.findById(id);
        int studentId = student.getId();
        System.out.println(studentId);
        //该学生学习课程的情况
        //获取未完成的课程id
        List<Integer> lessonIds = partyStudentLessonService.findIds(studentId);
        for (Integer lesson : lessonIds){
            System.out.println(lesson);
        }
        //通过未完成的id 查询未完成的课程
        List<String> unLearn = new ArrayList<>();
        for (int i=0;i<lessonIds.size();i++){
            PartyLesson lesson = partyLessonService.findById(lessonIds.get(i));
            unLearn.add(lesson.getName());
        }
        //获取完成的课程id
        List<Integer> haveIds = partyStudentLessonService.findHaveIds(studentId);
        //通过id 查询课程名
        List<String> haveLearn = new ArrayList<>();
        for(int i=0;i<haveIds.size();i++){
            PartyLesson lesson = partyLessonService.findById(haveIds.get(i));
            haveLearn.add(lesson.getName());
        }

        map.put("code",0);
        map.put("data",student);
        map.put("unLearn",unLearn);
        map.put("haveLearn",haveLearn);
        return map;
    }
    /**
     * @author  王宇
     * @date 2018/8/14
     * @description  保存视频文件
     */
    @PostMapping("/savevideo")
    @ResponseBody
    public Map<String,Object> savevideo(@Param("videoName") String videoName,
                                        @Param("lessonName") String lessonName,
                                        @Param("imgPath") String imgPath,
                                        @Param("videoPath") String videoPath){
        Map<String,Object> map = new HashMap<>();
//        System.out.println(videoName+"+"+lessonName+"+"+imgPath+"+"+videoPath);
        //通过课程名，拿到课程ID
        PartyLesson lesson = partyLessonService.findByName(lessonName);
        int lessonId = lesson.getId();
        imgPath = Nginx.URL + imgPath;
        lesson.setPic(imgPath);
        partyLessonService.save(lesson);
        videoPath = Nginx.URL + videoPath;
        PartyVideo video = new PartyVideo();
        video.setLessonId(lessonId);
        video.setPic(imgPath);
        video.setLessonPath(videoPath);
        video.setVideoName(videoName);
        videoService.save(video);

        map.put("code",0);
        return map;
    }



    /**
     * @author  王宇
     * @date 2018/9/3
     * @description  删除课程
     */
    @PostMapping("/deleteLesson")
    @ResponseBody
    public Map<String,Object> deleteLesson(@Param("id") Integer id){
        Map<String,Object> map = new HashMap<>();
        //删除课程
        partyLessonService.deleteLesson(id);
        map.put("code",0);
        map.put("msg","删除成功");
        return  map;
    }

    /**
     * @author  王宇
     * @date 2018/9/4
     * @description  删除学生
     */

    @PostMapping("/deleteStudent")
    @ResponseBody
    public Map<String,Object> deleteStudent(@Param("id") int id){
        Map<String,Object> map = new HashMap<>();

        //删除血红色呢过
        partyStudentService.delete(id);
        map.put("code",0);
        map.put("msg","删除成功");
        return map;
    }
}
